package com.liuhaoqi.raletiveblog.controller;

import com.github.pagehelper.PageInfo;
import com.liuhaoqi.raletiveblog.entity.Blog;
import com.liuhaoqi.raletiveblog.entity.Category;
import com.liuhaoqi.raletiveblog.entity.dto.BlogDto;
import com.liuhaoqi.raletiveblog.result.BlogPageResult;
import com.liuhaoqi.raletiveblog.result.WebRestResult;
import com.liuhaoqi.raletiveblog.result.business.PageResult.CategoryPageResult;
import com.liuhaoqi.raletiveblog.service.BlogService;
import com.liuhaoqi.raletiveblog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author liuhaoqi
 * @Date 2020/4/20 16:04
 * @Version 1.0
 */
@Controller
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    BlogService blogService;



    @GetMapping("/category")
    public String getCategoryByName(@RequestParam("categoryname") String categoryname, Model model,
                                    @RequestParam(defaultValue = "1",value = "pageNum") String pageNum,
                                    @RequestParam(defaultValue = "3",value = "pageSize") String pageSize) {
        //分类导航,查出所有分类返回给前端
        List<BlogDto> blogListNoFenYe = blogService.getBlogListNoFenYe();
        List<Category> categoryList = categoryService.getCategoryList();
        model.addAttribute("categoryList",categoryList);
        model.addAttribute("blogListNoFenYe",blogListNoFenYe);
        if (categoryname != null) {
            PageInfo<BlogDto> blogListByCategoryname = blogService.getBlogListByCategoryname(categoryname,pageNum,pageSize);
            model.addAttribute("blogListByCategoryname",blogListByCategoryname);
        }
        model.addAttribute("categoryname1",categoryname);
        return "category";
    }

    @GetMapping("/admin/getCategoryList")
    @ResponseBody
    public BlogPageResult<CategoryPageResult> getAdminCategoryList(@RequestParam(defaultValue = "1",value = "pageNum") int pageNum,
                                                                   @RequestParam(defaultValue = "10",value = "pageSize") int pageSize){
        int start=(pageNum-1)*pageSize;
        int end=pageSize;

        List<CategoryPageResult> adminCategoryList = categoryService.getAdminCategoryList(start,end,pageSize);
        BlogPageResult<CategoryPageResult> result = new BlogPageResult<>();
        result.setCode(0);
        result.setCount(adminCategoryList.get(0).getCnt());
        result.setMsg("查询分类成功！");
        result.setData(adminCategoryList);
        return result;
    }

    @PostMapping("/admin/deleteCategoryByCategoryId")
    @ResponseBody
    public WebRestResult deleteCategoryByCategoryId(@RequestParam("categoryId") Long categoryId) {
        int i = categoryService.deleteCategoryByCategoryId(categoryId);
        WebRestResult result = new WebRestResult();
        if (i > 0) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("删除类别成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除类别失败");
        }
        return result;
    }

    @PostMapping("/admin/addCategory")
    @ResponseBody
    public WebRestResult addCategory(@RequestParam("categoryname") String categoryname) {
        int i = categoryService.addCategory(categoryname);
        WebRestResult result = new WebRestResult();
        if (i > 0) {
            result.setResult(WebRestResult.SUCCESS);
            result.setMessage("增加分类成功！");
        } else {
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("增加分类失败！");
        }
        return result;
    }
}
